﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGenerator.CSharp
{
    /// <summary>
    /// Similar to the StringBuilder class but more specific for building up code.
    /// </summary>
    public class CodeBuilder
    {

        public CodeBuilder() { }
        public CodeBuilder(string text) { this.MySB = new StringBuilder(text); }

        private StringBuilder MySB = new StringBuilder();

        /// <summary>
        /// Appends the identified text to the code already contained in this builder without 
        /// appeneding a new line.
        /// </summary>
        public void Append(string text)
        {
            this.MySB.Append(text);
        }

        /// <summary>
        /// Appends the identified format/elements to the code already contained in this builder 
        /// without appeneding a new line.
        /// </summary>
        /// <param name="format"></param>
        /// <param name="elements"></param>
        public void Append(string format, params object[] elements)
        {
            this.MySB.Append(string.Format(format, elements));
        }

        /// <summary>
        /// Adds the identified text to the code already contained in this builder and appends a 
        /// new line character at the end.
        /// </summary>
        public void Add(string text)
        {
            this.MySB.AppendLine(text);
        }

        /// <summary>
        /// Adds the string representation of the identified code units.
        /// </summary>
        public void Add(CodeUnitCollection codeUnits)
        {
            this.Add(codeUnits.ToString());
        }

        /// <summary>
        /// Adds a string representation of the identified object.
        /// </summary>
        public void Add(object value)
        {
            this.Add(value.ToString());
        }

        /// <summary>
        /// Adds the identified format/elements to the code already contained in this builder 
        /// and appends a new line character at the end.
        /// </summary>
        public void Add(string format, params object[] elements)
        {
            this.MySB.AppendLine(string.Format(format, elements));
        }

        /// <summary>
        /// Adds an empty line.
        /// </summary>
        public void Add()
        {
            this.MySB.AppendLine();
        }


        public void AddIf(bool condition, string text)
        {
            if (condition) this.Add(text);
        }

        public void AddIf(bool condition, string format, params object[] elements)
        {
            if (condition) this.Add(format, elements);
        }

        public int Length
        {
            get { return this.MySB.Length; }
        }

        public void Remove(int startIndex, int length)
        {
            this.MySB.Remove(startIndex, length);
        }

        public override string ToString()
        {
            return this.MySB.ToString();
        }

    }
}
